package com.naiterui.ehp.bs.payment.repository;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bs.payment.entity.RefundInfo;

/**
 * 退款信息
 *
 * @date 2017年09月09日
 * @since 1.0.0
 */
@Repository
public interface IRefundInfoRepository extends BaseJpaRepository<RefundInfo, Long> {

    /**
     * 通过退款单号获取退款信息
     */
    RefundInfo findByRefundOrderSn(String refundOrderSn);

    /**
     * 通过退款单号获取最新的退款信息
     * 
     * @param refundOrderSn
     * @return
     */
    RefundInfo findFirstByRefundOrderSnOrderByCreatedAtDesc(String refundOrderSn);

    /**
     * 通过订单号更新订单状态
     *
     * @param refundOrderSn 退款单号
     * @param status        订单状态
     */
    @Modifying(clearAutomatically = true)
    @Query("update RefundInfo set status=:status where refundOrderSn=:refundOrderSn")
    void updateStatus(@Param("refundOrderSn") String refundOrderSn, @Param("status") Integer status);

    /**
     * 通过订单号更新订单退款通知状态
     *
     * @param refundOrderSn 退款单号
     * @param status        订单状态
     * @param failCode      错误编码
     * @param failMsg       错误代码描述
     */
    @Modifying(clearAutomatically = true)
    @Query("update RefundInfo set status=:status,failCode=:failCode,failMsg=:failMsg where refundOrderSn=:refundOrderSn")
    void updateStatus(@Param("refundOrderSn") String refundOrderSn, @Param("status") Integer status, @Param("failCode") String
            failCode, @Param("failMsg") String failMsg);

    /**
     * 通过订单号更新订单退款通知状态
     *
     * @param refundOrderSn        退款单号
     * @param notifyBusinessStatus 通知状态
     */
    @Modifying(clearAutomatically = true)
    @Query("update RefundInfo set notifyBusinessStatus=:notifyBusinessStatus where refundOrderSn=:refundOrderSn")
    void updateNotifyBusinessStatus(@Param("refundOrderSn") String refundOrderSn, @Param("notifyBusinessStatus") Integer notifyBusinessStatus);

}
